import { ExtractPropTypes, PropType } from "vue"
import { validatorColorValue } from '../../utils'
import { ZIndex } from '../../config'
import { UPDATE_MODEL_EVENT } from '../../constants'

/**
 * 列表模式有效值
 */
export const SelectModes = ['single', 'multi', 'autoMulti'] as const
export type SelectMode = typeof SelectModes[number]

/**
 * props参数
 */
export const selectProps = {
  /** modelValue, 控制select的显示、隐藏 */
  modelValue: {
    type: Boolean,
    default: false
  },
  /** 默认值, 对应选项的index下标 */
  defaultValue: {
    type: Array as PropType<any>,
    default: () => {
      return [0]
    }
  },
  /** 列表模式 */
  mode: {
    type: String,
    default: (): SelectMode => {
      return 'single'
    },
    validator: (val: SelectMode) => {
      return SelectModes.includes(val)
    }
  },
  /** 列表数据 */
  data: {
    type: Array as PropType<any[]>,
    default: () => {
      return []
    }
  },
  /** value属性名 */
  valueName: {
    type: String,
    default: 'value'
  },
  /** label属性名 */
  labelName: {
    type: String,
    default: 'label'
  },
  /** child属性名, 当autoMulti时有效 */
  childName: {
    type: String,
    default: 'children'
  },
  /** 取消按钮文本 */
  cancelText: {
    type: String,
    default: '取消'
  },
  /** 取消按钮文本颜色, 支持内置颜色的值、十六进制、rgb()、rgba()的值 */
  cancelColor: {
    type: String,
    default: '',
    validator: (value: string) => {
      return validatorColorValue('color', value)
    }
  },
  /** 确认按钮文本 */
  confirmText: {
    type: String,
    default: '确认'
  },
  /** 确认按钮文本颜色, 支持内置颜色的值、十六进制、rgb()、rgba()的值 */
  confirmColor: {
    type: String,
    default: '',
    validator: (value: string) => {
      return validatorColorValue('color', value)
    }
  },
  /** 显示遮罩 */
  mask: {
    type: Boolean,
    default: true
  },
  /** zIndex */
  zIndex: {
    type: [String, Number],
    default: ZIndex.popup
  }
}
export type SelectProps = ExtractPropTypes<typeof selectProps>

export const selectEmits = {
  [UPDATE_MODEL_EVENT]: (value: boolean) => typeof value === 'boolean',
  cancel: () => true,
  confirm: (value: any[]) => Array.isArray(value)
}
export type SelectEmits = typeof selectEmits
